import {create} from "zustand";
import {immer} from 'zustand/middleware/immer';
import {persist, devtools} from 'zustand/middleware';
import type {useUserStoreType, useSettersStoreType} from "@/typings/store";


const initialState: useUserStoreType.stateType = {
    token: '',
    userInfo: {}
}

const actions = (set: useSettersStoreType<useUserStoreType.storeType>) => {
    const setToken = (token: string) => {
        set((state: useUserStoreType.stateType) => {
            state.token = token
        })
    }
    const setUserInfo = (payload: Record<string, any>) => {
        set((state: useUserStoreType.stateType) => {
            state.userInfo = payload.userInfo
        })
    }

    return {setToken, setUserInfo}
}

const storage = {name: 'user-storage'}
export const useUserStore = create<useUserStoreType.storeType>()(devtools(persist(immer((set) => ({
    ...initialState,
    ...actions(set)
})), storage), storage))
